import moment from 'moment';
import {
    Toast
} from 'saltui';

const Utils = {
    /**
     * 根据value值获取字典对应的项
     * 字典格式如下
     * [
     *   {value: '0', text: '事假'}, {value: '1', text: '病假'},
     *   {value: '2', text: '产假'}, {value: '3', text: '年假'},
     * ]
     *
     * @param array  字典
     * @param val value值
     * @returns {*}
     */
    getZDItem: function(array, val) {
        if (array.length > 0) {
            var i = -1;
            array.map((item, index) => {
                if (item.value == val) {
                    i = index;
                }
            });
            return array[i];
        } else {
            return null;
        }

    },
    /**
     * 根据value值获取字典对应的项
     * 字典格式如下
     * [
     *   {value: '0', text: '事假'}, {value: '1', text: '病假'},
     *   {value: '2', text: '产假'}, {value: '3', text: '年假'},
     * ]
     *
     * @param array  字典
     * @param val value值
     * @returns {*}
     */
    getZDItemByText: function(array, val) {
        if (array.length > 0) {
            var i = -1;
            array.map((item, index) => {
                if (item.text == val) {
                    i = index;
                }
            });
            return array[i];
        } else {
            return null;
        }

    },
    /**
     * 将timestamp数值转换成指定的日期格式字符串
     *
     * @param timestamp 时间戳
     * @param format 日期格式,如:yyyy-MM-dd HH:mm:ss
     * @returns {*}
     */
    timestampFormat: function(timestamp, format) {
        var d = new Date(timestamp);
        var date = {
            "M+": d.getMonth() + 1,
            "d+": d.getDate(),
            "h+": d.getHours(),
            "H+": d.getHours(),
            "m+": d.getMinutes(),
            "s+": d.getSeconds(),
            "q+": Math.floor((d.getMonth() + 3) / 3),
            "S+": d.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (var k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
                    date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    },

    /**
     * 将格式化的日期转换为时间戳
     * @param formatDate
     * @returns {number}
     */
    formatDate2Timestamp(formatDate) {
        if (formatDate) {
            while (formatDate.indexOf('-') >= 0) {
                formatDate = formatDate.replace('-', '/');
            }
            var d = new Date(formatDate);
            var i = d.getTime();
            // var i = moment(formatDate, "yyyy-MM-dd HH:mm:ss").format('X');
            return i;
        } else {
            var d = new Date();
            var i = d.getTime();
            return i;
        }
    },

    /**
     * 通用的必填校验方法
     * @param  {[type]} form   [description]
     * @param  {[type]} config [description]
     * @return {[type]}        [true: 校验通过； false：校验不通过]
     */
    veifyCheck(form, config) {
        if (form && config instanceof Array) {
            for (var i = 0; i < config.length; i++) {
                if (form[config[i].field] == null || form[config[i].field] == undefined ||
                    form[config[i].field] == '' || form[config[i].field].length < 0) {
                    Toast.show({
                        type: 'error',
                        content: config[i].hint,
                        onHide: function() {}
                    });
                    return false;
                }
            }
        }
        return true;
    },

    /**
     * 将钉钉用户的信息装换成后台可以保持的scope结构
     * 
     * [transformScopeUserDd2Platform description]
     * @param  {[type]} users [description]
     * @return {[type]}       [description]
     */
    transformScopeUserDd2Platform(users) {
        let platform = [];
        if (users && users instanceof Array && users.length > 0) {
            users.map((item, index) => {
                let i = {
                    scopeType: "user",
                    scopeValue: item.key,
                    scopeName: item.label
                }
                platform.push(i);
            });
        }
        return platform;

    },

    /**
     * 将论坛中的帖子信息装换成html格式
     * 
     * [transformForumPostContent description]
     * @param  {[type]} content [description]
     * @return {[type]}         [description]
     */
    transformForumPostContent(content) {
        if (content) {
            let reg = new RegExp('\n', 'gm');
            let str = '<p>' + content.replace(reg, '</p><p>') + '</p>';
            return str;
        }
        return content;
    }

}

export default Utils;